<?php
/**
 * 作者: https://gitee.com/miren123
 * 时间: 2022-05-25 17:43:46
 * 描述: 地区管理
 */

namespace app\admin\controller;

use think\facade\Request;
use app\common\service\RegionService;
use app\common\validate\RegionValidate;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("地区")
 * @Apidoc\Group("adminSetting")
 * @Apidoc\Sort("70")
 */
class Region
{
    /**
     * @Apidoc\Title("地区列表")
     * @Apidoc\Header(ref="headerAdmin")
     * @Apidoc\Param(ref="paramPaging")
     * @Apidoc\Param("type", type="string", default="list", desc="返回的数据类型：list列表，tree树形")
     * @Apidoc\Param("region_pid", type="string", default="", desc="pid")
     * @Apidoc\Param("region_id", type="string", default="", desc="id")
     * @Apidoc\Param("region_name", type="string", default="", desc="名称")
     * @Apidoc\Param("region_pinyin", type="string", default="", desc="拼音")
     * @Apidoc\Returned(ref="returnPaging"),
     * @Apidoc\Returned("list", type="array", desc="数据列表", 
     *     @Apidoc\Returned(ref="app\common\model\RegionModel\list")
     * )
     */
    public function list()
    {
        $sort_field    = Request::param('sort_field/s ', '');
        $sort_type     = Request::param('sort_type/s', '');
        $type          = Request::param('type/s', 'list');
        $region_pid    = Request::param('region_pid/d', 0) ?: 0;
        $region_id     = Request::param('region_id/d', '');
        $region_name   = Request::param('region_name/s', '');
        $region_pinyin = Request::param('region_pinyin/s', '');

        if ($type == 'tree') {
            $data = RegionService::info('tree');
        } else {
            if ($region_id || $region_name || $region_pinyin) {
                if ($region_id) {
                    $where[] = ['region_id', '=', $region_id];
                }
                if ($region_name) {
                    $where[] = ['region_name', '=', $region_name];
                }
                if ($region_pinyin) {
                    $where[] = ['region_pinyin', '=', $region_pinyin];
                }
            } else {
                $where[] = ['region_pid', '=', $region_pid];
            }

            $order = [];
            if ($sort_field && $sort_type) {
                $order = [$sort_field => $sort_type];
            }

            $data = RegionService::list($where, $order);
        }

        return success($data);
    }

}
